﻿/*

Flash CS4 ActionScript 3 Tutorial by Dan Gries.

www.flashandmath.com.

Last modified September 17, 2009.

*/

package com.flashandmath.dg.objects {
	import flash.display.Sprite;
	import flash.geom.*;
	
	public class BubbleDrop extends Sprite {
				
		public var lifespan:int;
		public var breakawayTime:int;
				
		public var thickness:Number;
		
		public var pos:Point;		
		public var vel:Point;
		public var accel:Point;
		
		public var color:uint;
		
		//drawing points
		public var p0:Point;
		public var p1:Point;
		
		public var lastPos:Point;
		public var lastLastPos:Point;
		
		public var radiusWhenStill:Number;
		
		public var attack:Number;
		public var hold:Number;
		public var decay:Number;
		public var rInit:Number;
		public var rHold:Number;
		public var rLast:Number;
		
		public var dotAlpha:Number;
		public var dotAlphaVariance:Number;
		
		private var angle:Number;
		private var cos:Number;
		private var sin:Number;
		
		public var rotationInc:Number;
		
		//The following attributes are for the purposes of creating a
		//linked list of BubbleDrop instances.
		public var next:BubbleDrop;
		public var prev:BubbleDrop;
		
		public var splashing:Boolean;
		public var atTerminalVelocity:Boolean;
		
		public var dotRadius:Number;
		public var dotRadiusVariance:Number;
		
		public var alphaMin:Number;
		
		public var airResistanceFactor:Number;
		public var dead:Boolean;
		
		private var rad:Number;
		private var randomizedAlpha:Number;
		
		public function BubbleDrop(x0=0,y0=0) {
			super();
			lastPos = new Point(x0,y0);
			lastLastPos = new Point(x0,y0);
			pos = new Point(x0,y0);
			p0 = new Point(x0,y0);
			p1 = new Point(x0,y0);
			accel = new Point();
			vel = new Point();
			thickness = 1;
			color = 0xDDDDDD;
			splashing = true;
			atTerminalVelocity = false;
			radiusWhenStill = 1.5;
			dotRadius = 1;
			dotRadiusVariance = 1;
			alphaMin = 0;
			
			attack = 100+Math.random()*10;
			hold = 60+Math.random()*30;
			decay = 70+Math.random()*100;
			rInit = 0;
			rHold = 1.5;
			rLast = 0.5;
			
			dotAlpha = 1;
			dotAlphaVariance = 1;
			
			airResistanceFactor = 0.03;
			dead = false;
			angle = 0;
			rotationInc = (2*Math.random()-1)*Math.PI*2/64;
		}
		
		public function setEnvelope(a:Number, h:Number, d:Number, av:Number, hv:Number, dv:Number, r0:Number, r1:Number, r2:Number, rv:Number):void {
			attack = a+(2*Math.random()-1)*av;
			hold = h+(2*Math.random()-1)*hv;
			decay = d+(2*Math.random()-1)*dv;
			rInit = r0;
			rHold = r1;
			rLast = r2;
			dotRadiusVariance = rv;
		}
		
		public function resetPosition(x0=0,y0=0) {
			lastPos = new Point(x0,y0);
			lastLastPos = new Point(x0,y0);
			pos = new Point(x0,y0);
			p0 = new Point(x0,y0);
			p1 = new Point(x0,y0);
		}
		
		public function redraw():void {
			this.graphics.clear();
			if (lifespan < breakawayTime) {
				randomizedAlpha = alphaMin + dotAlpha*(1+(1-2*Math.random())*dotAlphaVariance);
				this.graphics.lineStyle(1,color, randomizedAlpha);
				this.graphics.beginFill(color, 0.8*randomizedAlpha);
				this.graphics.drawEllipse(p1.x-radiusWhenStill,p1.y-radiusWhenStill,2*radiusWhenStill,2*radiusWhenStill)
				this.graphics.endFill();
			}
			else {
				//dot:
				if (lifespan < breakawayTime+attack+hold+decay) {
					if (lifespan < breakawayTime+attack) {
						rad = (rHold - rInit)/attack*(lifespan-breakawayTime) + rInit;
					}
					else if (lifespan < breakawayTime+attack+hold) {
						rad = rHold;
					}
					else if (lifespan < breakawayTime+attack+hold+decay) {
						rad = (rLast - rHold)/decay*(lifespan-breakawayTime-attack-hold) + rHold;
					}
					rad = (1+dotRadiusVariance*(1-2*Math.random()))*rad;
					randomizedAlpha = alphaMin + dotAlpha*(1+(1-2*Math.random())*dotAlphaVariance);
					this.graphics.lineStyle(1,color, randomizedAlpha);
					this.graphics.beginFill(color, 0.8*randomizedAlpha);
					this.graphics.drawEllipse(p1.x-rad,p1.y-rad,2*rad,2*rad);
					this.graphics.endFill();					
				}
				else {
					dead = true;
				}
						
			}
		}
		
	}
}
			
		